<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Starunse_model extends CI_Model {
	
	function __construct() {
		parent::__construct();
	}

	/**
	 * 스타 상세 정보
	 */
	function get_artist($idx) {
		log_message('debug',"#### Starunse_model::get_artist($idx)");
		
		$this->db->where('idx', $idx);
		$query = $this->db->get('tb_artist') or $this->logsql->log_sql('error');
		$result = $query->row_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 스타 정보 수정
	 */
	function update_artist($artist) {
		log_message('debug',"#### Starunse_model::update_artist($artist)");
		
		$this->db->where('idx', $artist['idx']);
		$this->db->update('tb_artist', $artist) or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}
	
	/**
	 * 스타 등록
	 */
	function insert_artist($artist) {
		log_message('debug',"#### Starunse_model::insert_artist($artist)");
		
		$this->db->set('create_dt', 'now()', FALSE);
		$this->db->insert('tb_artist', $artist) or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}
	
	/**
	 * 스타 삭제
	 */
	function delete_artist($idx) {
		log_message('debug',"#### Starunse_model::delete_artist($idx)");

		$this->db->where('idx', $idx);
		$this->db->delete('tb_artist') or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}
	
	

	/**
	 * 스타 사주 정보 리스트
	 */
	function select_artist_list($keyword, $page_no, $page_size, $order, $sort) {
		log_message('debug',"#### Starunse_model::select_artist_list($keyword, $page_no, $page_size, $order, $sort)");
		
		if (empty($order)) {
			$order = 'idx';
		}

		if (empty($sort)) {
			$sort = 'desc';
		}
		
		$start_row = ($page_no-1) * $page_size;

		// $sql  = PHP_EOL." SELECT ";
		// $sql .= PHP_EOL."       idx ";
		// $sql .= PHP_EOL."     , artist_name ";
		// $sql .= PHP_EOL."     , sex ";
		// $sql .= PHP_EOL."     , birth_yyyy ";
		// $sql .= PHP_EOL."     , birth_mm ";
		// $sql .= PHP_EOL."     , birth_dd ";
		// $sql .= PHP_EOL." FROM  tb_artist ";
		// $sql .= PHP_EOL." ORDER BY idx ";
		// $sql .= PHP_EOL." LIMIT $start_row, $page_size ";
		
		$this->db->like('keyword', $keyword);
		$this->db->order_by($order, $sort);
		$this->db->limit($page_size,$start_row);
		$query = $this->db->get('tb_artist') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 총 내역 수
	 */
	function get_artist_total_count($keyword) {
		$this->db->like('keyword', $keyword);
		$this->db->from('tb_artist');
		$result = $this->db->count_all_results();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}
	
	/**
	 * 스타 사주 정보 리스트
	 */
	function select_artist_all_list() {
		log_message('debug',"#### Starunse_model::select_artist_all_list()");

		$sql  = PHP_EOL." SELECT ";
		$sql .= PHP_EOL."       idx ";
		$sql .= PHP_EOL."     , artist_name ";
		$sql .= PHP_EOL."     , sex ";
		$sql .= PHP_EOL."     , birth_yyyy ";
		$sql .= PHP_EOL."     , birth_mm ";
		$sql .= PHP_EOL."     , birth_dd ";
		$sql .= PHP_EOL." FROM  tb_artist ";
		$sql .= PHP_EOL." ORDER BY idx ";
		$query = $this->db->query($sql) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
//		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 운세 데이터DB에서 운세 결과를 가져온다.
	 * 결과는 JSON
	 */
	function get_db_express($userinfo) {
		log_message('debug',"#### Starunse_model::get_db_express($userinfo)");

		$url = "http://218.145.66.136/UNSE_DATA/api/star_unse_db_express.asp";
		$req = new HttpRequest($url, HttpRequest::METH_POST);
		$req->addPostFields($userinfo);

		try {
			$return_obj = json_decode($req->send()->getBody(), TRUE);
			$this->logsql->log_result($return_obj);
			return $return_obj;
		} catch (HttpException $ex) {
			die('## 운세데이터를 가져오는 중 에러 발생');
			echo $ex;
		}
	}
	
	/**
	 * db_express 등록
	 */
	function insert_artist_express($idx, $table_name, $db_express) {
		log_message('debug',"#### Starunse_model::insert_artist_express($idx, $table_name, $db_express)");

		$sql  = PHP_EOL." INSERT IGNORE INTO tb_artist_express ( ";
		$sql .= PHP_EOL."       idx ";
		$sql .= PHP_EOL."     , table_name ";
		$sql .= PHP_EOL."     , db_express ";
		$sql .= PHP_EOL."     , create_dt ";
		$sql .= PHP_EOL." ) VALUES ( ";
		$sql .= PHP_EOL."       ? ";
		$sql .= PHP_EOL."     , ? ";
		$sql .= PHP_EOL."     , ? ";
		$sql .= PHP_EOL."     , now() ";
		$sql .= PHP_EOL." ) ";
		$this->db->query($sql, array($idx, $table_name, $db_express)) or $this->logsql->log_sql('error');
	}
}

/* End of file starunse_model.php */
/* Location: ./application/models/star/starunse_model.php */